package com.upc.feedish.ui.locate;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.TextView;

import com.upc.feedish.R;
import com.upc.feedish.utils.DataBaseController;


public class LocateFragment extends Fragment {


    private View root;
    private TextView textView;
    private GridLayout locate_grid;
    private DataBaseController dataBaseController;
    Handler handler;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        dataBaseController=new DataBaseController();
        // Inflate the layout for this fragment
        if(root==null){
            root=inflater.inflate(R.layout.fragment_locate,container,false);
        }
        locate_grid = root.findViewById(R.id.locate_grid);
        textView = new TextView(new ContextThemeWrapper(this.getActivity(), R.style.TextViewTimeTable));

        handler=new Handler(Looper.getMainLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                Double[] d ;
                d=(Double[]) msg.obj;
                int x= (int) (d[0]*2);
                int y= (int) (d[1]*2);
                locate_grid.removeView(textView);
                showLocation(x%10+(x/10)*9,y%10+(y/10)*9);
                return false;
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Double[] doubles= dataBaseController.getLocation();
                        Message msg=new Message();
                        msg.obj=doubles;
                        handler.sendMessage(msg);
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


        return root;
    }

    public void showLocation(int row , int column){

        //CardView cardView = new CardView(new ContextThemeWrapper(this.getActivity(), R.style.CardViewClass));
        //cardView.setCardBackgroundColor(Color.RED);

        textView.setBackgroundColor(Color.RED); //设置背景颜色为红色
        textView.setText("("+(row+1)+","+(column+1)+")"); //设置显示的坐标
        //cardView.addView(textView);

        GridLayout.Spec rowSpec = GridLayout.spec(row,1);  //设置网格坐标的行
        GridLayout.Spec columnSpec = GridLayout.spec(column,1);  //设置网格坐标的列
        GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(rowSpec,columnSpec);
        layoutParams.setGravity(Gravity.FILL);
//        layoutParams.height = 0;
//        layoutParams.width = 0;
        locate_grid.addView(textView,layoutParams);

    }





}